package com.jf.nep.scene;

import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

import com.jf.nep.MathUtil;
import com.jf.nep.render.MeshInstance;

public class MeshInstanceImpl implements MeshInstance {
    private String meshId;
    private String programId;
    private Matrix4f modelMatrix = new Matrix4f();
    private Matrix4f rotationMatrix = new Matrix4f();
    private Vector3f position = new Vector3f(0,0,0);
    private Vector3f scale = new Vector3f(1,1,1);

    @Override
    public String getMeshId() {
        return meshId;
    }

    public void setMeshId(String meshId) {
        this.meshId = meshId;
    }
    
    public void setProgramId(String programId) {
        this.programId = programId;
    }

    @Override
    public Matrix4f getModelMatrix() {
        // TODO Auto-generated method stub
        return modelMatrix;
    }

    @Override
    public Matrix4f getRotationMatrix() {
        // TODO Auto-generated method stub
        return rotationMatrix;
    }
    
    public Vector3f getPosition() {
        return position;
    }
    
    public Vector3f getScale() {
        return scale;
    }

    public void rotateScaleAndTranslate(Vector3f angle, Vector3f scale, Vector3f translation) {
        Matrix4f rotationMatrix = getRotationMatrix();
        rotationMatrix.setIdentity();
        Matrix4f.rotate(MathUtil.degreesToRadians(angle.z), new Vector3f(0,0,1), rotationMatrix, rotationMatrix);
        Matrix4f.rotate(MathUtil.degreesToRadians(angle.y), new Vector3f(0,1,0), rotationMatrix, rotationMatrix);
        Matrix4f.rotate(MathUtil.degreesToRadians(angle.x), new Vector3f(1,0,0), rotationMatrix, rotationMatrix);
        
        scaleAndTranslate(scale, translation);
    }

    public void rotate(Vector3f angle) {
        rotateScaleAndTranslate(angle, getScale(), getPosition());        
    }
    
    public void scaleAndTranslate(Vector3f scale, Vector3f translation) {
        this.scale = scale;
        this.position = translation;
        Matrix4f rotationMatrix = getRotationMatrix();
        Matrix4f modelMatrix = getModelMatrix();
        modelMatrix.setIdentity();
        Matrix4f.translate(translation, modelMatrix, modelMatrix);
        Matrix4f.mul(modelMatrix, rotationMatrix, modelMatrix);
        Matrix4f.scale(scale, modelMatrix, modelMatrix);
    }

    @Override
    public String getProgramId() {
        return programId;
    }

    
}
